﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.uicontrols;
using System.Data.SqlClient;

namespace MailQueueUmbracoAdmin
{
    public partial class MailQueueItemDetails : System.Web.UI.UserControl
    {
        protected Guid _Id = Guid.Empty;
        protected TabView _tabViewContainer = null;

        protected Guid MailProcessId = Guid.Empty;
        protected DateTime MailCreateTime = DateTime.MinValue;
        protected DateTime MailProcessTime = DateTime.MinValue;
        protected int MailStatus = -1;
        protected int Priority = -1;
        protected int MailFormat = -1;
        protected string Sender = String.Empty;
        protected string FromName = String.Empty;
        protected string FromMailAddress = String.Empty;
        protected string ToMailAddress = String.Empty;
        protected string CCList = String.Empty;
        protected string BccList = String.Empty;
        protected string Subject = String.Empty;
        protected string Body = String.Empty;
        protected int ForeignKey1 = -1;
        protected int ForeignKey2 = -1;
        protected string ForeignKey3 = String.Empty;
        protected string MessageLabel = String.Empty;
        protected int RetryCount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            GetMessageInformation();
            DisplayMessageInformation();
        }

        protected void DisplayMessageInformation()
        {
            if (Subject.Length == 0)
                Subject = "[No subject]";

            lblSubject.Text = Subject;
            lblBCC.Text = BccList;
            lblCC.Text = CCList;
            lblCreated.Text = MailCreateTime.ToString();
            lblFromMailAddress.Text = FromMailAddress;
            lblFromName.Text = FromName;
            lblProcessedTime.Text = MailProcessTime.ToString();
            lblToMailAddress.Text = ToMailAddress;
            
            if (MailFormat==1)
                lblBody.Text = "<pre>" + Body + "</pre>";
            else
                lblBody.Text = Body;
        }

        protected void GetMessageInformation()
        {
            string sqlCmdText = "SELECT [MailQueueID] " +
              " ,[MailProcessId] " +
              " ,[MailCreateTime] " +
              " ,[MailProcessTime] " +
              " ,[MailStatus] " +
              " ,[Priority] " +
              " ,[MailFormat] " +
              " ,[Sender] " +
              " ,[FromName] " +
              " ,[FromMailAddress] " +
              " ,[ToMailAddress] " +
              " ,[CCList] " +
              " ,[BccList] " +
              " ,[Subject] " +
              " ,[Body] " +
              " ,[ForeignKey1] " +
              " ,[ForeignKey2] " +
              " ,[ForeignKey3] " +
              " ,[Label] " +
              " ,[RetryCount] " +
            " FROM [MailQueue] WHERE [MailQueueID] = @Id";

            SqlCommand cmd = new SqlCommand();
            cmd.Parameters.AddWithValue("@Id", this.UniqueMailId);
            cmd.CommandText = sqlCmdText;
            cmd.CommandType = System.Data.CommandType.Text;

            SqlDataReader dr = DbHelper.DbHelper.ExecuteSqlReader(cmd, System.Configuration.ConfigurationManager.ConnectionStrings["mailqueue"].ConnectionString);

            using (dr)
            {
                while (dr.Read())
                {
                    for (int i = 1; i < 20; i++)
                    {
                        if (!dr.IsDBNull(i))
                        {
                            switch (i)
                            {
                                case 1:
                                    MailProcessId = (Guid)dr[i];
                                    break;
                                case 2:
                                    MailCreateTime = (DateTime)dr[i];
                                    break;
                                case 3:
                                    MailProcessTime = (DateTime)dr[i];
                                    break;
                                case 4:
                                    MailStatus = (int)dr[i];
                                    break;
                                case 5:
                                    Priority = (int)dr[i];
                                    break;
                                case 6:
                                    MailFormat = (int)dr[i];
                                    break;
                                case 7:
                                    Sender = (string)dr[i];
                                    break;
                                case 8:
                                    FromName = (string)dr[i];
                                    break;
                                case 9:
                                    FromMailAddress = (string)dr[i];
                                    break;
                                case 10:
                                    ToMailAddress = (string)dr[i];
                                    break;
                                case 11:
                                    CCList = (string)dr[i];
                                    break;
                                case 12:
                                    BccList = (string)dr[i];
                                    break;
                                case 13:
                                    Subject = (string)dr[i];
                                    break;
                                case 14:
                                    Body = (string)dr[i];
                                    break;
                                case 15:
                                    ForeignKey1 = (int)dr[i];
                                    break;
                                case 16:
                                    ForeignKey2 = (int)dr[i];
                                    break;
                                case 17:
                                    ForeignKey3 = (string)dr[i];
                                    break;
                                case 18:
                                    MessageLabel = (string)dr[i];
                                    break;
                                case 19:
                                    RetryCount = (int)dr[i];
                                    break;
                            }
                        }
                    }
                }
            }

            dr.Close();
        }

        public Guid UniqueMailId
        {
            get { return _Id; }
            set { _Id = value; }
        }

        public TabView TabViewContainer
        {
            get { return _tabViewContainer; }
            set { _tabViewContainer = value; }
        }

        protected void closeBtn_Click(object sender, EventArgs e)
        {
            TabViewContainer.Controls.Remove(this.Parent);
        }
    }
}